
#include "TUIDisplay.h"


char strNoCHOn[]="please turn on the channel first.";

int TUI_dispRUNState();
int TUI_dispIconUnit(unsigned char unitID);
int TUI_dispPage(unsigned char pageID);
int TUI_dispData(unsigned short addr,long val, char type);
int TUI_dispCHOn(char OnOff);
int TUI_dispInfo(unsigned short addr ,const char* buffer,unsigned short len);
int TUI_dispCHPage(char pageCH);
int TUI_dispCH(unsigned char CHItem);
int TUI_dispPreview(CHStatus m_CHStatus);
int TUI_dispAllStatus();


int TUI_dispPage(unsigned char pageID)
{
  MSG80_SEND msg2 =TUI_defMsg80();
  msg2.addr = 0x03;
  msg2.data[0] = 0;
  msg2.data[1] = pageID;
  return TUI_sendMsg((unsigned char*)(&msg2),7);
}

int TUI_dispHome()
{
  int i =0;
  TUI_dispIconUnit(m_UserConfig.PresUnit);
  for(i=0;i<6;i++)
  {
    TUI_dispData(0x0110+ i*0x10,m_TUIStatus.CHs[i].nowPres.value_act,TUI_DATA_PRESSURE);
    TUI_dispData(0x0114+ i*0x10,m_TUIStatus.CHs[i].nowPres.value_set,TUI_DATA_PRESSURE);
  }
  return 1;
}

int TUI_dispCH(unsigned char CHItem)
{
  int i=0;
  m_TUIStatus.CHItem = CHItem;
  m_TUIStatus.nowPage = 1;

  MSG82_SEND msg = TUI_defMsg82();
  msg.addr[0] = 0x10;
  msg.addr[1] = 0x08;
  msg.data[0] = 0;
  msg.data[1] = m_TUIStatus.CHItem;
  TUI_sendMsg((unsigned char*)(&msg),8);
  TUI_dispCHOn(m_TUIStatus.CHs[m_TUIStatus.CHItem].isON);
  
  for(i=0; i<0x0F;i++)
  {
    TUI_dispData(0x0400+i*0x10,m_TUIStatus.CHs[m_TUIStatus.CHItem].Value[i].value_set,TUI_DATA_PRESSURE);
    TUI_dispData(0x0300+i*0x10,m_TUIStatus.CHs[m_TUIStatus.CHItem].Value[i].value_act,TUI_DATA_PRESSURE);
  }
  TUI_dispPage(PAGE_CH);
}

int TUI_dispCHPage(char pageCH)
{
  MSG80_SEND msg = TUI_defMsg80();
  msg.addr = 0x03;
  msg.data[0] = 0;
  if(pageCH == 2)
    msg.data[1] = PAGE_CH_2;
  else if(pageCH == 3)
    msg.data[1] = PAGE_CH_3;
  else if(pageCH == 4)
    msg.data[1] = PAGE_CH_4;
  else
    msg.data[1] = PAGE_CH;
  return TUI_sendMsg((unsigned char*)(&msg),7);
}

int TUI_dispCHOn(char OnOff)
{
  MSG82_SEND msg = TUI_defMsg82();
  msg.addr[0] = 0x03;
  msg.addr[1] = 0xf0;
  msg.data[0] = 0;
  msg.data[1] = OnOff;
  return TUI_sendMsg((unsigned char*)(&msg),8);
}
int TUI_dispRUNState()
{
  MSG82_SEND msg = TUI_defMsg82();
  msg.addr[0] = 0x00;
  msg.addr[1] = 0x12;
  msg.data[0] = 0;
  msg.data[1] = m_TUIStatus.runState;
  return TUI_sendMsg((unsigned char*)(&msg),8);
}
int TUI_dispIconUnit(unsigned char unitID)
{
  MSG82_SEND msg = TUI_defMsg82();
  msg.addr[0] = 0x01;
  msg.addr[1] = 0x04;
  msg.data[0] = 0;
  msg.data[1] = unitID;
  return TUI_sendMsg((unsigned char*)(&msg),8);
}
int TUI_dispData(unsigned short addr,long val, char type)
{
  MSG82_SEND msg = TUI_defMsg82();
  if(type == TUI_DATA_PRESSURE)
    val = val / unitScale[m_UserConfig.PresUnit];
  msg.addr[0] = (addr >> 8);
  msg.addr[1] = addr & 0xff;
  msg.data[0] = val >> 24;
  msg.data[1] = val >> 16;
  msg.data[2] = val >> 8;
  msg.data[3] = val & 0xff;
  msg.length  = 0x07;
  return TUI_sendMsg((unsigned char*)(&msg),10);
}
int TUI_dispInfo(unsigned short addr ,const char* buffer,unsigned short len)
{
  int i=0;
  MSG82_SEND msg = TUI_defMsg82();
  msg.addr[0] = (addr >> 8);
  msg.addr[1] = addr & 0xff;
  for(i=0;i<len;i++)
  {
    msg.data[i]=buffer[i];
  }
  msg.length  = 3+len;
  return TUI_sendMsg((unsigned char*)(&msg),6+len);
}
int TUI_dispPreview(CHStatus m_CHStatus)
{
  short x_zero = 48;
  short y_zero = 153,y_step = 30;
  int i =0,j=0,k=0;
  char levelOrder[5]={1,1,1,1,1};
  long CHsetVal[5];
  short y[5];
  short Rate[5],Time[9];
  MSG82_SEND msg = TUI_defMsg82();
  msg.addr[0] = 0x11;
  msg.addr[1] = 0x10;
  msg.data[0] = 0x00;// line cmd
  msg.data[1] = 0x02;
  msg.data[4] = 0xf8;// color red
  msg.data[5] = 0x00;

  CHsetVal[0] = m_CHStatus.Value[0].value_set;
  Time[0]     = m_CHStatus.Value[1].value_set;
  for(i=1;i<5;i++)
  {
    CHsetVal[i] = m_CHStatus.Value[i*3].value_set;
    Rate[i]     = m_CHStatus.Value[i*3-1].value_set;
    if(Time[i*2] == 0)
      Time[i*2] = Time[i*2-2]+2;
    if(Rate[i] != 0 )
      Time[i*2-1] = Time[i*2-2]+abs(CHsetVal[i]-CHsetVal[i-1])/Rate[i];
    else
      Time[i*2-1] = Time[i*2-2]+1;

    Time[i*2] = m_CHStatus.Value[i*3+1].value_set + Time[i*2-1];

    if(m_CHStatus.Value[i*3-1].value_set == 0 && k ==0)
      k = i;
  }
  for(i=0;i<5;i++)
  {
    for(j=i+1;j<5;j++)
    {
      if(CHsetVal[i] > CHsetVal[j])
        levelOrder[i] ++;
      else
        levelOrder[j] ++;
    }
  }

  y[0] = y_zero - y_step*(levelOrder[0]-1);

  msg.data[6] = x_zero >> 8;
  msg.data[7] = x_zero & 0xff;
  msg.data[8] = y[0] >> 8;
  msg.data[9] = y[0] & 0xff;

  for(i=1;i<k;i++)
  {
    y[i] = y_zero - y_step*(levelOrder[i]-1);

    msg.data[2+i*8] = (x_zero + 33*(2*i-1)) >> 8;
    msg.data[3+i*8] = (x_zero + 33*(2*i-1)) & 0xff;
    msg.data[4+i*8] = y[i] >> 8;
    msg.data[5+i*8] = y[i] & 0xff;

    msg.data[6+i*8] = (x_zero + 33*(2*i)) >> 8;
    msg.data[7+i*8] = (x_zero + 33*(2*i)) & 0xff;
    msg.data[8+i*8] = y[i] >> 8;
    msg.data[9+i*8] = y[i] & 0xff;
  }
  msg.length =5+i*8;

  msg.data[2] = 0x00;// line num
  msg.data[3] = i*2 -2;

  TUI_sendMsg((unsigned char*)(&msg),8+i*8); //draw the whole line

  for(i=0;i<9;i++)
  {
    msg.addr[0] = 0x06;
    msg.addr[1] = i*2;
    msg.data[0] = Time[i] >> 8;
    msg.data[1] = Time[i]  & 0xff;
    msg.length  = 5;
    TUI_sendMsg((unsigned char*)(&msg),8);
  }
  for(i=0;i<5;i++)
  {
    unsigned short pre = CHsetVal[i];
    if(pre == 0)
      pre = levelOrder[i]-1;
    msg.addr[0] = 0x06;
    msg.addr[1] = 0x10 + levelOrder[i]*2;
    msg.data[0] = pre >> 8;
    msg.data[1] = pre & 0xff;
    msg.length  = 5;
    TUI_sendMsg((unsigned char*)(&msg),8);

  }
  return 1;
}
int TUI_dispAllStatus()
{
  int i=0,j=0;
  long val;
  StepInfo* CH_val;
  MSG82_SEND msg = TUI_defMsg82();

  if(m_TUIStatus.readyState != m_TUIlastStatus.readyState)
  {
    msg.addr[0] = 0x10;
    msg.addr[1] = 0x00;
    msg.data[0] = 0;
    msg.data[1] = m_TUIStatus.runState;
    TUI_sendMsg((unsigned char*)(&msg),8);
  }
  if(m_TUIStatus.runState != m_TUIlastStatus.runState)
  {
    TUI_dispRUNState();
  }
  if(m_TUIStatus.temperature != m_TUIlastStatus.temperature)
  {
    msg.addr[0] = 0x10;
    msg.addr[1] = 0x06;
    msg.data[0] = m_TUIStatus.temperature >> 8;
    msg.data[1] = m_TUIStatus.temperature & 0xff;
    TUI_sendMsg((unsigned char*)(&msg),8);
  }
  i = m_TUIStatus.CHItem;
  if(m_TUIStatus.CHs[i].isON != m_TUIlastStatus.CHs[i].isON)
    TUI_dispIcon(0x03f0,m_TUIStatus.CHs[i].isON);

  for(j=0;j<TUI_CH_NUM;j++)
  {
    if(m_TUIStatus.CHs[j].setON != m_TUIlastStatus.CHs[j].setON)
      TUI_setCHOn(j,m_TUIStatus.CHs[j].setON);//send to SEPC
    if(m_TUIStatus.CHs[j].nowPres.value_act != m_TUIlastStatus.CHs[j].nowPres.value_act)
      TUI_dispData(0x0110+ j*0x10,m_TUIStatus.CHs[j].nowPres.value_act,TUI_DATA_PRESSURE);
    if(m_TUIStatus.CHs[j].nowPres.value_set != m_TUIlastStatus.CHs[j].nowPres.value_set)
      TUI_dispData(0x0114+ j*0x10,m_TUIStatus.CHs[j].nowPres.value_set,TUI_DATA_PRESSURE);
    if(m_TUIStatus.CHs[j].Value[0].value_set != m_TUIlastStatus.CHs[j].Value[0].value_set && m_TUIlastStatus.CHs[j].isON == 1)
      TUI_setCH_Pressure(j,m_TUIStatus.CHs[j].Value[0].value_set);
    else if(m_TUIlastStatus.CHs[j].isON == 0 && m_TUIStatus.CHs[j].isON == 1)
      TUI_setCH_Pressure(j,m_TUIStatus.CHs[j].Value[0].value_set);
  }

  CH_val = m_TUIStatus.CHs[i].Value;
  CH_val[14].value_set = CH_val[1].value_set;
  for(j=0;j<4;j++)
  {
    if(CH_val[j*3+2].value_set>0)
      CH_val[14].value_set+= abs(CH_val[j*3+3].value_set-CH_val[j*3].value_set)/CH_val[j*3+2].value_set + CH_val[j*3+4].value_set;
    else
      CH_val[j*3+3].value_set = CH_val[j*3+4].value_set = 0;
  }

//   if(m_TUIStatus.CHs[i].Value[0].value_set != m_TUIlastStatus.CHs[i].Value[0].value_set && m_TUIlastStatus.CHs[i].isON == 1)
//     TUI_setCH_Pressure(i,m_TUIStatus.CHs[i].Value[0].value_set);
  for(j=0;j<15;j++)
  {
    val =(m_TUIStatus.CHs[i].Value[j].value_set);
    if(val != (m_TUIlastStatus.CHs[i].Value[j].value_set))
    {
      TUI_dispData(0x0400+ j*0x10,val,TUI_DATA_PRESSURE);
    }
  }
  val =(m_TUIStatus.CHs[i].Value[0].value_act);
  if(val != (m_TUIlastStatus.CHs[i].Value[0].value_act))
  {
    TUI_dispData(0x0300,val,TUI_DATA_PRESSURE);
  }
  m_TUIStatus.waitTime = m_TUIStatus.waitTime +1;
  if(m_TUIStatus.waitTime >= 60)
  {
    m_TUIStatus.waitTime = 60;
    if( m_TUIStatus.waitTime != m_TUIlastStatus.waitTime && m_UserConfig.LockPswd == 1)
    {
      TUI_dispPage(PAGE_WAITING);
      mLog("TUI_dispPage(PAGE_WAITING)\n");
    }
  }
  m_TUIlastStatus =m_TUIStatus;
  return 1;
}
int TUI_dispIcon(unsigned short addr,unsigned char iconID)
{
  MSG82_SEND msg = TUI_defMsg82();
  msg.addr[0] = addr >>8;
  msg.addr[1] = addr & 0xff;
  msg.data[0] = 0;
  msg.data[1] = iconID;
  return TUI_sendMsg((unsigned char*)(&msg),8);
}
